package com.gxkj.projects.myshopx.controllers.admin;

import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.utils.ListPager;
import com.gxkj.projects.myshopx.dto.ReturnData;
import com.gxkj.projects.myshopx.entitys.*;
import com.gxkj.projects.myshopx.enums.*;
import com.gxkj.projects.myshopx.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/admin/common")
public class AdminCommonController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private SuppliersService suppliersService;

    @Autowired
    private AttributeService attributeService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value="/goodstype/list",method= RequestMethod.GET)
    @ResponseBody
    public ReturnData<List<GoodType>> goodstypeList(

            HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){

        ReturnData<List<GoodType>> returnData = new ReturnData<List<GoodType>>();
        GoodType entity = new GoodType();
        entity.setStates(GoodTypeStates.ENABLE);
        int pageno = 0 ;
        int pagesize = Integer.MAX_VALUE;


        ListPager<GoodType> pager =  goodsTypeService.doPageHQL(pageno,pagesize,entity);
        returnData.setEntity(pager.getPageData());
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="/category/list",method= RequestMethod.GET)
    @ResponseBody
    public ReturnData<List<Category>> categoryList(
            HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){

        ReturnData<List<Category>> returnData = new ReturnData<List<Category>>();
        Category entity = new Category();
        entity.setStates(CategoryState.NORMAL);
        int pageno = 0 ;
        int pagesize = Integer.MAX_VALUE;
        ListPager<Category>  pager = categoryService.doPageHQL(pageno,pagesize,entity);
        returnData.setEntity(pager.getPageData());
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="/brand/list",method= RequestMethod.GET)
    @ResponseBody
    public ReturnData<List<Brand>> brandList(
            HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){

        ReturnData<List<Brand>> returnData = new ReturnData<List<Brand>>();
        Brand entity = new Brand();
        entity.setStates(BrandStates.NORMAL);
        int pageno = 0 ;
        int pagesize = Integer.MAX_VALUE;
        ListPager<Brand>  pager = brandService.doPageHQL(pageno,pagesize,entity);
        returnData.setEntity(pager.getPageData());
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="/suppliers/list",method= RequestMethod.GET)
    @ResponseBody
    public ReturnData<List<Suppliers>> suppliersList(
            HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){

        ReturnData<List<Suppliers>> returnData = new ReturnData<List<Suppliers>>();
        Suppliers entity = new Suppliers();
        entity.setStates(SuppliersStates.NORMAL);
        int pageno = 0 ;
        int pagesize = Integer.MAX_VALUE;
        ListPager<Suppliers>  pager = suppliersService.doPageHQL(pageno,pagesize,entity);
        returnData.setEntity(pager.getPageData());
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }
    @RequestMapping(value="/goods/goodsAttribute",method= RequestMethod.GET)
    @ResponseBody
    public  ReturnData< List<Attribute> >goodAtribute(String goodtypeid,
                                                      HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData< List<Attribute> > returnData = new ReturnData< List<Attribute> >();
        List<Attribute> attributeList = attributeService.getAttributeListByTypeId(goodtypeid);
        returnData.setEntity(attributeList);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="/goods/get_goods_list",method= RequestMethod.GET)
    @ResponseBody
    public  ReturnData< List<Goods> >get_goods_list( int pageno, int pagesize,Goods entity,
                                                      HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData< List<Goods> > returnData = new ReturnData< List<Goods> >();
        ListPager<Goods> pageGoods = goodsService.doPageHQL(pageno,pagesize,entity);
        returnData.setEntity(null);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }



}
